<template>
  <div>
    <el-card class="user-list">
      <div slot="header" class="clearfix">
        <el-form ref="form" :inline="true" :model="search" label-position="top">
          <el-form-item label="手机号">
            <el-input
              placeholder="请输入手机号"
              v-model="search.phone"
            ></el-input>
          </el-form-item>
          <el-form-item label="注册时间">
            <el-col :span="9">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="search.startCreateTime"
                style="width: 100%"
              ></el-date-picker>
            </el-col>
            <el-col class="line" :span="2">-</el-col>
            <el-col :span="9">
              <el-date-picker
                placeholder="选择日期"
                v-model="search.endCreateTime"
                style="width: 100%"
              ></el-date-picker>
            </el-col>
            <el-col :span="2">
              <el-button class="search-btn" @click="onSearch">查询</el-button>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="userList" style="width: 100%">
        <el-table-column prop="id" label="用户ID" width="180" align="center">
        </el-table-column>
        <el-table-column prop="xxx" label="头像" width="180" align="center">
        </el-table-column>
        <el-table-column prop="name" label="用户名" width="180" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="手机号" align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="注册时间" align="center">
        </el-table-column>
        <el-table-column prop="isDel" label="状态" align="center">
          <template slot-scope="scope">
            <div class="status-wrap">
              <div :class="scope.row.status ? 'icon user-valid' : 'icon'"></div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleForbidden(scope.row)"
              >禁用</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleAllocateRole(scope.row)"
              >分配角色</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :disabled="loading"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="search.current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="search.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </el-card>
    <allocate-role
      :show="dialogVisible"
      :userId="userId"
      @changeDialog="changeDialog"
    ></allocate-role>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import dayjs from 'dayjs'
import { getUserPages, forbidUser } from '@/apis/user'
import AllocateRole from './allocate-role.vue'
export default Vue.extend({
  name: 'user-list',
  components: {
    AllocateRole
  },
  data() {
    return {
      search: {
        currentPage: 1,
        pageSize: 10,
        phone: null,
        userId: null,
        startCreateTime: '',
        endCreateTime: ''
      },
      userList: [],
      pagination: {
        total: 0
      },
      dialogVisible: false,
      userId: '',
      loading: false
    }
  },
  created() {
    this.loadUserLIst()
  },
  methods: {
    async loadUserLIst() {
      this.loading = true
      const { data } = await getUserPages(this.search)
      if (data.code === '000000') {
        this.userList = data.data.records.map((item: any) => ({
          id: item.id,
          phone: item.phone,
          name: item.name,
          createTime: dayjs(item.createTime).format('YYYY-MM-DD HH:MM:ss'),
          isDel: item.isDel,
          status: item.status === 'ENABLE'
        }))
        this.pagination.total = data.data.total
      }
      this.loading = false
    },
    onSearch() {
      this.search.currentPage = 1
      this.loadUserLIst()
    },
    async handleForbidden(row: any) {
      console.log('submit!')
      this.userId = row.id
      this.$confirm('是否要禁用该用户？', '删除用户', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async() => {
          const { data } = await forbidUser(row.id)
          if (data.code === '000000') {
            this.$message.success('删除用户成功')
            this.loadUserLIst()
          }
        })
        .catch(() => {
          this.$message.info('取消删除用户')
        })
    },
    handleAllocateRole(row: any) {
      console.log('submit!')
      this.userId = row.id
      this.changeDialog(true)
    },
    changeDialog(state: boolean) {
      this.dialogVisible = state
    },
    handleSizeChange(val: number) {
      this.search.pageSize = val
      this.search.currentPage = 1
      this.loadUserLIst()
    },
    handleCurrentChange(val: number) {
      this.search.currentPage = val
      this.loadUserLIst()
    }
  }
})
</script>

<style lang="scss" scoped>
.user-list {
  .search-btn {
    margin-left: 20px;
  }
  .line {
    text-align: center;
  }
  .el-pagination {
    padding-top: 20px;
  }
  .status-wrap {
    display: flex;
    justify-content: center;
    .icon {
      width: 10px;
      height: 10px;
      background-color: red;
      border-radius: 50%;
    }
    .user-valid {
      background-color: green;
    }
  }
}
</style>
